package medium最长连续序列;

import org.testng.annotations.Test;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class Solution {
    public int longestConsecutive(int[] nums) {
        if (nums.length == 0)
            return 0;
        Arrays.sort(nums);
        int max = 0;
        int record = 1;
        int start = nums[0];
        for (int i = 1; i < nums.length; i++) {
            System.out.println(nums[i]);
            if (nums[i] - start == 0){
                continue;
            }
            else if (nums[i] - start == 1){
                start = nums[i];
                record++;
            }
            else{
                start = nums[i];
                max = Math.max(max,record);
                record = 1;
            }
        }
        return Math.max(max,record);
    }
    public int longestConsecutive1(int[] nums) {
        Set<Integer> set = new HashSet<>();
        for (int i:
                nums) {
            set.add(i);
        }
        int longestStreak = 0;

        for (int num : set) {
            if (!set.contains(num - 1)) {
                int currentNum = num;
                int currentStreak = 1;

                while (set.contains(currentNum + 1)) {
                    currentNum += 1;
                    currentStreak += 1;
                }

                longestStreak = Math.max(longestStreak, currentStreak);
            }
        }
        return longestStreak;
    }
    @Test
    public void test(){
        int[] num = {0,3,7,2,5,8,4,6,0,1};
        System.out.println(longestConsecutive(num));
    }
}
